<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pembayaran_psb extends CI_Controller {
	
	var $objectid = 'pembayaran_psb';
	
	function __construct(){
		parent::__construct();
		$this->load->model('sd/setting/mdl_pembayaran_psb');
		$this->load->model('sd/setting/mdl_proses_penerimaan');
		$this->load->model('sd/setting/mdl_kelompok_penerimaan');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data['objectid'] = $this->objectid;
		$this->load->view('sd/setting/pembayaran_psb', $data);
	}
	
	function grid(){
		$data = $this->mdl_pembayaran_psb->get_data();
		echo $this->mdl_pembayaran_psb->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['objectid'] = $this->objectid;
		
		$data['id'] = '';
		$data['posisi'] = '';
		$data['pembayaran'] = '';
		$data['nominal'] = '';
		
		$this->load->view('sd/setting/pembayaran_psb_form', $data);
	}
	
	function edit($id){
		$data['objectid'] = $this->objectid;
		
		$r = $this->mdl_pembayaran_psb->get_data_edit($id);
		
		$data['id'] = $id;
		
		$data['posisi'] = $r->row()->posisi;
		$data['pembayaran'] = $r->row()->pembayaran;
		$data['nominal'] = $r->row()->nominal;
		
		$this->load->view('sd/setting/pembayaran_psb_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules('posisi', 'Posisi', 'trim|numeric|required|xss_clean');
		$this->form_validation->set_rules('pembayaran', 'Pembayaran', 'trim|required|xss_clean');
		$this->form_validation->set_rules('nominal', 'Nominal', 'trim|numeric|required|xss_clean');
        
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi dengan angka.');
		$this->form_validation->set_message('email', 'Field %s harus diisi dengan alamat email yang benar.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"].= trim(form_error('posisi',' ',' '))==''?'':form_error('posisi',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('pembayaran',' ',' '))==''?'':form_error('pembayaran',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('nominal',' ',' '))==''?'':form_error('nominal',' ',' ').'<br>';
			
		}else{
			if($aksi=="add"){ // add
				$result = $this->mdl_pembayaran_psb->insert($data);
				
			}else { // edit
				$result=$this->mdl_pembayaran_psb->update($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_pembayaran_psb->delete($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
}